package com.example.livingadminmaster.controller;

import com.example.livingadminmaster.pojo.Maintenance;
import com.example.livingadminmaster.pojo.StatusRequest;
import com.example.livingadminmaster.service.MaintenanceServoice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/maintenance")
public class MaintenanceContrioller {
    @Autowired
    private MaintenanceServoice maintenanceServoice;

    /**
     * 获取所有维修人员信息列表
     * @return
     */
    @ResponseBody
    @GetMapping("/AllMaintenance")
    public List<Maintenance> getAllMaintenance() {
        List list= maintenanceServoice.getAllMaintenance();
        return list;
    }

    /**
     * 根据维修人员id获取维修人员信息
     * @param maintenance_id
     * @return
     */
    @GetMapping("/{maintenance_id}")
    public Maintenance getMaintenanceById(@PathVariable long maintenance_id) {
        return maintenanceServoice.getMaintenanceById(maintenance_id);
    }

    /**
     * 根据维修人员用户名获取维修人员信息
     * @param username
     * @return
     */
    @GetMapping("/selectByUsername")
    public Maintenance getMaintenanceByUsername(@RequestParam String username) {
        return maintenanceServoice.getMaintenanceByUsername(username);
    }

    /**
     * 添加维修人员信息
     * @param maintenance
     */
    @PostMapping("/addMaintenance")
    @ResponseBody
    public void addMaintenance(@RequestBody Maintenance maintenance) {
        maintenanceServoice.addMaintenance(maintenance);
    }

    /**
     * 删除单个维修工信息
     * @param maintenance_id
     */
    @DeleteMapping("/deleteMaintenanceById")
    @ResponseBody
    public void deleteMaintenanceById(@RequestParam long maintenance_id) {
        maintenanceServoice.deleteMaintenanceById(maintenance_id);
    }

    /**
     * 更新维修人员信息
     * @param maintenance_id
     * @param maintenance
     * @return
     */
    @PutMapping("/updateMaintenance/{maintenance_id}")
    public String updateMaintenance(@PathVariable long maintenance_id, @RequestBody Maintenance maintenance) {
        if(maintenance.getMaintenanceId() != maintenance_id) {
            return "维修人员ID不匹配";
        }
        int result = maintenanceServoice.updateMaintenance(maintenance);
        if (result > 0) {
            return "维修人员信息更新成功";
        } else {
            return "维修人员信息更新失败";
        }
    }

    /**
     * 更新维修人员状态
     * @param maintenance_id
     * @param status
     */
    @PutMapping("/{maintenance_id}/updateMaintenanceStatus")
    public void updateMaintenanceStatus(@PathVariable long maintenance_id, @RequestBody StatusRequest status) {
        maintenanceServoice.updateMaintenanceStatus(maintenance_id, status.getStatus());
    }

}
